<?php

$actorID;
$action;
$level;
$typ;
$code;
$num;

if (file_exists("config.xml")) {
	$config_xml = simplexml_load_file("config.xml");
} else {
	exit("Konnte Datei nicht laden. ");
}

if (isset($_GET["actorID"])) {
	$actorID = $_GET["actorID"];
} else {
	$actorID = '001';
}
if (isset($_GET["action"])) {
	$action = $_GET["action"];
} else {
	
	$action = 'OFF';
}
if (isset($_GET["level"])) {
	$level = $_GET["level"];
} else {
	$level = 0;
} 

// $quelltext = file_get_contents("http://192.168.178.1:55000/cmd=IT A 1 ON");
// Pfad für Ligthmanager setzen
$path="/config/General/LightManager";
$LMPathXML=$config_xml->xpath($path);
$address=$LMPathXML[0]->URL;
$port=$LMPathXML[0]->PORT;
$path='/config/Scenes/Scene[@sortID=\''.$actorID.'\']';
$ScenesXML=$config_xml->xpath($path);
$cmd = "";
$result = "";
$fp=fsockopen($address, (int) $port, $errno, $errstr);

if($fp) {
// Read welcome string
//	fread($fp,64);
// Attribute zu diesem Aktor besorgen

	$path = '/config/Actors/Actor[@ID=\'' . $actorID . '\']';
	$actor_xml = $config_xml -> xpath($path);
	$typ = (string)$actor_xml[0] -> Typ;
	$code = (string)$actor_xml[0] -> Code;
	$num = (string)$actor_xml[0] -> Num;
	$typ = str_replace(' ', '', $typ);
	$code = str_replace(' ', '', $code);
	$num= str_replace(' ', '', $num);
	if ($action != 'DIM') {
		$cmd = $typ . ' ' . $code . ' ' . $num . ' ' . $action;
	} else {
		$cmd = $typ . ' ' . $code . ' ' . $num . ' ' . $level;
	}
	fwrite($fp,$cmd);
			// End of command
	fwrite($fp,"\n");
		// Return result
	$result=$result.fread($fp,16).PHP_EOL;
			// Finally read command prompt
	fread($fp,8);
	fclose($fp);
} else {
	$result = "Konnte keine Verbindung zum LM herstellen";	
}
echo $result;	

?>